package com.xdf.whiteaccount.controller;

import com.xdf.whiteaccount.utils.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.xdf.whiteaccount.entity.GjrkbillCp;
import com.xdf.whiteaccount.service.GjrkbillCpService;
import com.xdf.whiteaccount.utils.JsonResult;

/**
 * 注意要有依赖：
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger2</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger-ui</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 * 同时入口类头上使用注解：@EnableSwagger2
 * @Description : 控制层，代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2023-12-1 10:11:33
 */
@Api(tags = "")
@CrossOrigin
@RestController
@RequestMapping("/gjrkbillCp")
public class GjrkbillCpController {
    @Autowired
    private GjrkbillCpService service;

    // 全部属性新增
    @ApiOperation(value = "完全新增", httpMethod = "POST", notes = "推荐使用save请求")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody GjrkbillCp record){
        return JsonResult.ok(service.insert(record));
    }

    // 选择属性新增。value=null 的属性不会写入DB，DB会填充默认值
    @ApiOperation(value = "选择性新增", httpMethod = "POST", notes = "推荐使用save请求")
    @PostMapping("/insertSelective")
    public JsonResult insertSelective(@RequestBody GjrkbillCp record){
        return JsonResult.ok(service.insertSelective(record));
    }

    // 根据主键删除，切记表要有主键，否则为删除所有数据！！！！！！！！！！
    @ApiOperation(value = "根据主键删除", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/delete/{pk}")
    public JsonResult deleteByPrimaryKey(@PathVariable Integer pk){
        return JsonResult.ok(service.deleteByPrimaryKey(pk));
    }

    // 根据主键查询，切记表要有主键，否则为查询所有数据！！！！！！！！！！
    @ApiOperation(value = "根据主键查询", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/select/{pk}")
    public JsonResult selectByPrimaryKey(@PathVariable Integer pk){
        return JsonResult.ok(service.selectByPrimaryKey(pk));
    }

    // 获取所有数据
    @ApiOperation(value = "查询所有", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/selectAll")
    public JsonResult selectAll(){
        return JsonResult.ok(service.selectAll());
    }

    // 全部属性更新。前端传递来属性value=null则会设置后台数据为null
    @ApiOperation(value = "完全修改", httpMethod = "POST", notes = "推荐使用save方法")
    @PostMapping("/updateByPrimaryKey")
    public JsonResult updateByPrimaryKey(@RequestBody GjrkbillCp record) {
        return JsonResult.ok(service.updateByPrimaryKey(record));
    }

    // 部分属性更新。前端传递来value=null的属性不会被更新到数据库
    @ApiOperation(value = "选择性修改", httpMethod = "POST", notes = "推荐使用save方法")
    @PostMapping("/updateByPrimaryKeySelective")
    public JsonResult updateByPrimaryKeySelective(@RequestBody GjrkbillCp record) {
        return JsonResult.ok(service.updateByPrimaryKeySelective(record));
    }

    // 有则修改，无则新增。选择性保存，如果属性value=null则不会更新该字段
    @ApiOperation(value = "保存数据", httpMethod = "POST", notes = "主键为空则新增(选择性新增)，主键有值则修改（选择性修改）")
    @PostMapping("/save")
    public JsonResult save(@RequestBody GjrkbillCp record) {
        return JsonResult.ok(service.save(record));
    }

    // 是上面接口 save 的批量执行版本
    @ApiOperation(value = "对象数组保存", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/multiSave")
    public JsonResult multiSave(@RequestBody List<GjrkbillCp> records) {
        return JsonResult.ok(service.multiSave(records));
    }
    //=================以上是自动生成的代码，自定义代码请写在下面==================

    @ApiOperation(value = "根据主键批量查询", httpMethod = "POST", notes = "传入参数是色布打卷表的主键构成的数组")
    @PostMapping("/selectByPrimaryKeyList")
    public JsonResult selectByPrimaryKeyList(@RequestBody List<Integer> idList) {
        return JsonResult.ok(service.selectByPrimaryKeyList(idList));
    }

    @ApiOperation(value = "扫描打卷单匹二维码", httpMethod = "POST", notes = "扫描打卷单匹二维码，更新目标数据b_upload为设备编号后返回打卷明细数据")
    @PostMapping("/scanOneQR")
    public JsonResult scanOneQR(@RequestBody Map<String, Integer> params) {
        Integer jrkId = params.get("jrkId");
        Integer deviceId = params.get("deviceId");
        return JsonResult.ok(service.scanOneQR(jrkId,deviceId));
    }

    @ApiOperation(value = "扫描货架二维码", httpMethod = "POST", notes = "扫描货架二维码，批量更新目标数据b_upload为设备编号后返回打卷明细数据数组")
    @PostMapping("/scanShelvesQR")
    public JsonResult scanShelvesQR(@RequestBody Map<String, Object> params) {
        String shelvesQR = (String)params.get("shelvesQR");
        Integer deviceId = (Integer) params.get("deviceId");
        return JsonResult.ok(service.scanShelvesQR(shelvesQR,deviceId));
    }

    @ApiOperation(value = "根据主键清除扫描标记", httpMethod = "POST", notes = "传入参数：{\"bItemID\":11}")
    @PostMapping("/clearPDADeviceId8PrimaryKey")
    public JsonResult clearPDADeviceId8PrimaryKey(@RequestBody Map<String,Integer> record) {
        return JsonResult.ok(service.clearPDADeviceId8PrimaryKey(record.get("bItemID")));
    }

    @ApiOperation(value = "根据货架号清除扫描标记", httpMethod = "POST", notes = "传入参数：{\"bBarcode\":\"100012\"}")
    @PostMapping("/clearPDADeviceId8Barcode")
    public JsonResult clearPDADeviceId8Barcode(@RequestBody Map<String,String> record) {
        String bBarcode = Optional.ofNullable(record.get("bBarcode")).orElse("");
        if(bBarcode.length() == 0) throw new BusinessException("传入货架号为空，请确认通过属性bBarcode设置！");
        return JsonResult.ok(service.clearPDADeviceId8Barcode(bBarcode));
    }

    @ApiOperation(value = "根据扫描的流水号清除扫描标记", httpMethod = "POST", notes = "传入参数：{\"scanId\":11}")
    @PostMapping("/clearPDADeviceId8ScanId")
    public JsonResult clearPDADeviceId8ScanId(@RequestBody Map<String, Integer> record) {
        return JsonResult.ok(service.clearPDADeviceId8ScanId(record.get("scanId")));
    }

    @ApiOperation(value = "从发货单中剔除一匹数据，不会删除打卷数据", httpMethod = "POST", notes = "传入参数：{\"jrkId\":12}")
    @PostMapping("/clearFPDIDOne")
    public JsonResult clearFPDIDOne(@RequestBody Map<String, Integer> record) {
        Integer jrkId = record.get("jrkId");
        return JsonResult.ok(service.clearFPDIDOne(jrkId));
    }
}